home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_show / knight.e < prev    next >
Text File  |  1997-04-13  |  4KB  |  176 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class KNIGHT
  5. --|
  6. --| In this program a knight try to go over the n*n squares of a 
  7. --| chessboard without pass by the same square again.
  8. --| The position of the knigth is given by a number.
  9. --|  
  10. --|  Auteur: Christophe ALEXANDRE
  11. --|  date : Thu Mar 21 21:34:29 1996
  12. --
  13. -- Here is an example of solution on a 7 X 7 chesboard,
  14. -- knigth starting at position <1,1> :
  15. --                 ----------------------
  16. --                 | 1|28|37|24| 3|26|17|
  17. --                 ----------------------
  18. --                 |36|39| 2|27|18|11| 4|
  19. --                 ----------------------
  20. --                 |29|42|23|38|25|16| 9|
  21. --                 ----------------------
  22. --                 |40|35|30|19|10| 5|12|
  23. --                 ----------------------
  24. --                 |43|22|41|32|15| 8|47|
  25. --                 ----------------------
  26. --                 |34|31|20|45|48|13| 6|
  27. --                 ----------------------
  28. --                 |21|44|33|14| 7|46|49|
  29. --                 ----------------------
  30. --
  31.  
  32. inherit ANY redefine print_on end;
  33.    
  34. creation {ANY}
  35.    make
  36.  
  37. feature {ANY}
  38.  
  39.    make is
  40.       local
  41.      size, line, column: INTEGER;
  42.       do
  43.      size := ask("Enter the chess-board size : ",3,99);
  44.      line := ask("Enter the start line : ",1,size);
  45.      column := ask("enter the start column : ",1,size);
  46.      knight(size,line,column)
  47.       end;
  48.  
  49. feature {NONE}
  50.  
  51.    chessboard: ARRAY2[INTEGER];
  52.  
  53.    nb_tries: INTEGER;
  54.    
  55.    tl: ARRAY[INTEGER] is 
  56.       once
  57.      Result := <<-2,-1,1,2,2,1,-1,-2>>;
  58.       end;
  59.    
  60.    tc: ARRAY[INTEGER] is
  61.       once
  62.      Result := <<1,2,2,1,-1,-2,-2,-1>>;
  63.       end;
  64.      
  65.    knight(size, line, column: INTEGER) is
  66.       require
  67.      size >= 3;
  68.      1 <= line;
  69.      line <= size;
  70.      1 <= column;
  71.      column <= size;
  72.       do
  73.      !!chessboard.make(1,size,1,size); 
  74.      chessboard.put(1,line,column);
  75.      if solution(line,column) then
  76.         print_on(std_output);
  77.      else
  78.         io.put_string("Sorry, there is no solution.%N");
  79.      end;
  80.      io.put_string("%NNumber of tries : ");
  81.      io.put_integer(nb_tries);
  82.      io.put_new_line;
  83.       end; -- knight
  84.    
  85.    solution(line,column:INTEGER):BOOLEAN is
  86.       local
  87.      value,i : INTEGER;
  88.       do
  89.      if chessboard.count = chessboard.item(line,column) then
  90.         Result := true;
  91.      else
  92.         from  
  93.            i := 1;           
  94.            value := chessboard.item(line,column);
  95.         until
  96.            Result or else i > 8 
  97.         loop
  98.            Result := try(line+tl.item(i),column+tc.item(i),value);
  99.            i := i + 1;
  100.         end;
  101.      end;
  102.       end; -- solution
  103.    
  104.    try(line,column,value:INTEGER): BOOLEAN is
  105.      -- try to place the knight by used cross back-tracking method
  106.       do
  107.      nb_tries := nb_tries + 1;
  108.      if chessboard.valid_index(line,column) then
  109.         if chessboard.item(line,column) = 0 then
  110.            chessboard.put(value+1,line,column);
  111.            Result := solution(line,column);      
  112.            if not Result then
  113.           chessboard.put(0,line,column)
  114.            end;
  115.         end;           
  116.      end
  117.       end; -- try
  118.    
  119.    ask(s:STRING; min, max: INTEGER):INTEGER is 
  120.      -- ask a question until its answer is all right
  121.       local
  122.      stop: BOOLEAN;
  123.       do
  124.      from
  125.      until
  126.         stop
  127.      loop
  128.         io.put_string(s);
  129.         io.read_integer;
  130.         Result := io.last_integer;
  131.         if Result < min then
  132.            io.put_string("Value too small.%N");
  133.         elseif max < Result then
  134.            io.put_string("Value too big.%N");
  135.         else
  136.            stop := true;
  137.         end;
  138.      end;
  139.       end; -- ask
  140.       
  141.    print_on(file:STD_FILE_WRITE) is
  142.      -- display the cheesboard 
  143.       local
  144.      line,column : INTEGER;
  145.      separator : STRING;
  146.       do
  147.      from  
  148.         !!separator.make(3*chessboard.upper1+1);
  149.         separator.fill_with('-');
  150.         separator.extend('%N')
  151.         file.put_string(separator);
  152.         line := chessboard.lower1        
  153.      until
  154.         line > chessboard.upper1
  155.      loop
  156.         from  
  157.            column := chessboard.lower2           
  158.         until
  159.            column > chessboard.upper2
  160.         loop
  161.            if chessboard.item(line,column) < 10 then
  162.           file.put_string("| "); 
  163.            else
  164.           file.put_character('|');
  165.            end;
  166.            file.put_integer(chessboard.item(line,column));
  167.            column := column + 1;
  168.         end;
  169.         file.put_string("|%N");
  170.         file.put_string(separator);
  171.         line := line + 1;
  172.      end;
  173.       end; -- print_on
  174.   
  175. end -- KNIGHT
  176.